package com.siyuan2;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.util.Collections;

/**
 * @className: ApplicationConfig
 * @Description: TODO
 * @author: wzq
 * @date: 2022/8/25 13:51
 */
@ComponentScan(value = "com.siyuan2",excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = UserService.class)},
		includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = OrderService.class)}
)
public class ApplicationConfig {

	/**
	 * 类型的转换 ConversionService
	 *
	 * conversionService() 固定写法
	 * @return
	 */
	@Bean
	public ConversionServiceFactoryBean conversionService() {
		ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
		conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToUserConverter()));
		return conversionServiceFactoryBean;
	}

	/**
	 * 类型的转换 PropertyEditor
	 *
	 * customEditorConfigurer() 固定写法
	 * @return
	 */
//	@Bean
//	public CustomEditorConfigurer customEditorConfigurer() {
//		CustomEditorConfigurer configurer = new CustomEditorConfigurer();
//		Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();
//		propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
//		configurer.setCustomEditors(propertyEditorMap);
//		return configurer;
//	}

	/**
	 * 接收一个事件
	 * @return
	 */
	@Bean
	public ApplicationListener applicationListener() {
		return new ApplicationListener<>() {
			@Override
			public void onApplicationEvent(ApplicationEvent event) {
				System.out.println("接收一个事件");
			}
		};
	}

	@Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		// 指定文件名
		messageSource.setBasenames("messages");
		return messageSource;
	}

}
